package com.self.eduorder.service.impl;

import com.self.commonutils.ordervo.CourseWebVoOrder;
import com.self.commonutils.ordervo.UcenterMemberOrder;
import com.self.eduorder.client.CourseClient;
import com.self.eduorder.client.UcenterClient;
import com.self.eduorder.entity.Order;
import com.self.eduorder.mapper.OrderMapper;
import com.self.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.self.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author xiaozeng
 * @since 2020-10-09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public String createOrders(String courseId, String memberId) {
        //1.通过远程调用获取课程信息（通过Id）
        UcenterMemberOrder memberOrder = ucenterClient.getUserInfoOrder(memberId);

        //2.通过远程调用获取用户信息（通过Id）
        CourseWebVoOrder courseInfoOrder = courseClient.getCourseInfoOrder(courseId);

        //创建Order对象，向Order对象中设置值，并保存入数据库
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());//订单号
        order.setCourseId(courseId);//课程Id
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(memberOrder.getMobile());
        order.setNickname(memberOrder.getNickname());

        order.setStatus(0);//支付状态 订单状态（0：未支付，1：已支付）
        order.setPayType(1);//微信:1

        order.setGmtCreate(new Date());
        order.setGmtModified(new Date());

        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
